package LinkedList;

//给定一个已排序的链表的头 head ， 删除所有重复的元素，使每个元素只出现一次 。返回 已排序的链表 。
public class LeetCode_83_deleteDuplicates {
    public static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }


    public static ListNode deleteDuplicates(ListNode head) {
        if(head == null)return null;
        // 创建哑节点作为结果链表的起点
        ListNode dummy = new ListNode(0);
        ListNode result = dummy;

        while (head != null){
            if (result == dummy || head.val != result.val){
                result.next = new ListNode(head.val);
                result = result.next;
            }
            head = head.next;
        }

        return dummy.next;
    }

    public static void main(String[] args) {
        ListNode listNode = new ListNode(1);
        listNode.next = new ListNode(1);
        listNode.next.next = new ListNode(2);
        deleteDuplicates(listNode);

    }




}
